home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / SETEX.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  62 lines

  1.  #include <set>
  2.  
  3.  using namespace std;
  4.  
  5.  typedef set<double,less<double> > set_type;
  6.  
  7.  ostream& operator<<(ostream& out, const set_type& s)
  8.  {
  9.    copy(s.begin(), s.end(), ostream_iterator<set_type::value_type>(cout," "));
  10.    return out;
  11.  }
  12.  
  13.  int main ()
  14.  {
  15.    //
  16.    // Create a set of double's, and one of integers.
  17.    //
  18.    set_type   sd;
  19.    int         i;
  20.  
  21.    for (i = 0; i < 10; ++i)
  22.      //
  23.      // Insert values.
  24.      //
  25.      sd.insert(i);
  26.    //
  27.    // Print out the set.
  28.    //
  29.    cout << sd << endl << endl;
  30.    //
  31.    // Now let's erase half of the elements in the set.
  32.    //
  33.    int half = sd.size() / 2;
  34.    set_type::iterator sdi = sd.begin();
  35.    advance(sdi,half);
  36.    sd.erase(sd.begin(),sdi);
  37.    //
  38.    // Print it out again.
  39.    //
  40.    cout << sd << endl << endl;
  41.    //
  42.    // Make another set and an empty result set.
  43.    //
  44.    set_type sd2, sdResult;
  45.    for (i = 1; i < 9; i++)
  46.       sd2.insert(i+5);
  47.    cout << sd2 << endl;
  48.    //
  49.    // Try a couple of set algorithms.
  50.    //
  51.    set_union(sd.begin(),sd.end(),sd2.begin(),sd2.end(),
  52.              inserter(sdResult,sdResult.begin()));
  53.    cout << "Union:" << endl << sdResult << endl;
  54.  
  55.    sdResult.erase(sdResult.begin(),sdResult.end());
  56.    set_intersection(sd.begin(),sd.end(), sd2.begin(),sd2.end(),
  57.                     inserter(sdResult,sdResult.begin()));
  58.    cout << "Intersection:" << endl << sdResult << endl;
  59.  
  60.    return 0;
  61.  }
  62.